const axios = require('axios')

let querystring = (name) => {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return (r[2]); return null;
}

let get = (url, para) => {
    //+ '?__t=' + new Date().getTime()
    return axios.post(url, { params: para }).then(res => {
        return res.data
    }).catch(err => {
        alert('系统错误，请联系管理员！')
    })
}

let post = (url, para) => {
    //+ '?__t=' + new Date().getTime()
    return axios.post(url, para).then(res => {
        return res.data
    }).catch(err => {
        alert(err + '系统错误，请联系管理员！')
    })
}

axios.defaults.baseURL = `/api`
axios.defaults.headers.common['token'] = sessionStorage.getItem('token')

let user = {
    login(para) {
        return post('/account/loginDD', para).then(ret => {
            if (ret.success) {
                sessionStorage.setItem('isLogin', true)
            }
            return ret
        })
    },
    list(para) {
        return post('/user/', para)
    },
    detail(para) {
        return post('/user/detail', para)
    },
    dict(){
        return post('/table/dictionary/')
    },
    power(){
        return post('/user/getUserInfo/')
    },
}

let repair = {
    add(para) {
        return post('/repair/add', para)
    },
    index(para) {
        return post('/repair', para)
    },
    detail(para) {
        return post('/repair/detail', para)
    },
    send(para) {
        return post('/repair/send', para)
    },
    receive(para) {
        return post('/repair/receive', para)
    },
    doRepair(para) {
        return post('/repair/do', para)
    },
    eval(para) {
        return post('/repair/eval', para)
    },
    sign(para) {
        return post('/repair/sign', para)
    },
    getSign(para) {
        return post('/table/repair_sign/', para)
    },
    getWorkflow(para) {
        return post('/repair/workflow/', para)
    },
}

let file = {
    upload(file) {
        //let file = e.target.files[0];
        let param = new FormData(); //创建form对象
        param.append('file', file, file.name);//通过append向form对象添加数据
        //param.append('chunk', '0');//添加form表单中其他数据
        let config = {
            headers: { 'Content-Type': 'multipart/form-data' }
        };  //添加请求头
        return axios.post('/file/upload', param, config).then(res => res.data)
    }
}

let customer = {
    save(para) {
        return post('/table/customer/save', para)
    },
    detail(para) {
        return post('/table/customer/findOne', para)
    },
    index(para) {
        return post('/customer', para)
    }
}

let contact = {
    save(para) {
        return post('/table/contact/save', para)
    },
    index(para) {
        return post('/table/contact/findAll', para)
    },
    detail(para) {
        return post('/table/contact/findOne', para)
    },
    remove(para) {
        return post('/table/contact/remove', para)
    },
}

let batch = {
    save(para) {
        return post('/batch/save', para)
    },
    index(para) {
        return post('/batch/', para)
    },
}

let product = {
    index(para) {
        return post('/table/product/findAll', para)
    },
}

module.exports = {
    user,
    repair,
    file,
    customer,
    contact,
    batch,
    product,
}